package com.ermace.robo.jumper.game;

import java.util.HashMap;
import java.util.Map;

import android.content.Context;
import android.content.SharedPreferences;
import android.media.AudioManager;
import android.media.SoundPool;
import android.media.SoundPool.OnLoadCompleteListener;

import com.ermace.robo.jumper.R;
import com.ermace.robo.jumper.game.Kill.Combo;
import com.ermace.robo.jumper.game.Kill.KillSpree;

public class Sound {

	private static volatile Sound instance = null;
//	private static boolean init = false;
	
	private SoundPool soundPool;
	private boolean loaded = false;
	private final Map<Integer, Integer> soundMap;
	private final Map<Integer, Integer> killList = new HashMap<Integer, Integer>();
	private final Map<Integer, Integer> menu = new HashMap<Integer, Integer>();
	private int spreeOffset = 0;
	
	private final Integer SOUND_KILL_ID = 1;
	private final Integer SOUND_JUMP_ID = 2;
	private final Integer MUSIC_ID = 0;

	private final Integer SLIDE_ID = 0;
	
	private static float volume = 1;
	private SharedPreferences parameters;

	private Sound(Context context)
	{
		soundPool = new SoundPool(10, AudioManager.STREAM_MUSIC, 0);
		soundPool.setOnLoadCompleteListener(completeListener);	
		
		soundMap = new HashMap<Integer, Integer>();
		
		for (Combo combo: Combo.values()) {
			int soundId = combo.getSoundId();
			if (soundId != -1) {
				killList.put(combo.ordinal(), soundId);
				spreeOffset = combo.ordinal() + 1; //TODO zien of values() de juiste volgorde teruggeeft.
			}
		}
		
		for (KillSpree spree: KillSpree.values()) {
			int soundId = spree.getSoundId();
			if (soundId != -1) {
				killList.put(spree.ordinal() + spreeOffset, soundId);
			}
		}
		
		soundMap.put(SOUND_KILL_ID, soundPool.load(context, R.raw.death_explosion, 1));
		soundMap.put(SOUND_JUMP_ID, soundPool.load(context, R.raw.jump, 1));
	}

	public boolean isComplete() {
		return loaded;
	}
	
	public static synchronized Sound getInstance(Context context)
	{
		if (instance == null) {
			instance = new Sound (context);
		}
		return instance;
	}
	
	public static void setVolume(float vol)
	{
		volume = vol;
	}
	
	public void slide() {
		soundPool.play(menu.get(SLIDE_ID), 10*volume, 10*volume, 1, 0, 1.5f);
	}
	
	public void jumpSound()
	{
		soundPool.play(soundMap.get(SOUND_JUMP_ID), volume, volume, 1, 0, 1f);
	}
	
	public void killSound()
	{
		soundPool.play(soundMap.get(SOUND_KILL_ID), volume, volume, 1, 0, 1f);
	}
	
	public void playMusic()
	{
		soundPool.play(soundMap.get(MUSIC_ID), volume, volume, 2, 0, 1f);
	}
	
	public void comboSound(Combo combo)
	{
		try {
			soundPool.play(killList.get(combo.ordinal()), volume, volume, 1, 0, 1);
		} catch (NullPointerException e) {
			
		}
	}
	
	public void killSpreeSound(KillSpree spree)
	{
		try {
			soundPool.play(killList.get(spree.ordinal() + spreeOffset), volume, volume, 1, 0, 1);
		} catch (NullPointerException e) {
			
		}
	}
	
	private OnLoadCompleteListener completeListener = new OnLoadCompleteListener() {
		
		@Override
		public void onLoadComplete(SoundPool soundPool, int sampleId, int status) {
			if (sampleId == SOUND_JUMP_ID)
				loaded = true;
		}
	};
}
